package gcontent.entities.content;

import gcontent.entities.Content;
import gcontent.entities.content.error.ContentEx;
import java.util.HashMap;
import java.util.Iterator;

public class ContentManager {
	
	private static HashMap<String, Class<?>> content_list = new HashMap<String, Class<?>>();
	
	static {
		try { 
			init();
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private static void init() throws ContentEx
	{
		ContentManager.registerContent(gcontent.entities.content.pdf.ContentPDF.MIMETYPE_PDF, gcontent.entities.content.pdf.ContentPDF.class);
		ContentManager.registerContent(gcontent.entities.content.text.ContentText.MIMETYPE_TEXT, gcontent.entities.content.text.ContentText.class);
		ContentManager.registerContent(gcontent.entities.content.html.ContentHTML.MIMETYPE_HTML, gcontent.entities.content.html.ContentHTML.class);
		ContentManager.registerContent(gcontent.entities.content.gzip.ContentGZIP.TYPE_GZIP, gcontent.entities.content.gzip.ContentGZIP.class);
		//ContentManager.registerContent(gcontent.entities.content.dns.ContentDNS.TYPE_DNS, gcontent.entities.content.dns.ContentDNS.class);
		ContentManager.registerContent(gcontent.entities.content.image.ContentImage.TYPE_IMAGE, gcontent.entities.content.image.ContentImage.class);
		ContentManager.registerContent(gcontent.entities.Content.TYPE_ANY, gcontent.entities.Content.class);	
	}
	
	/* get a Content class from url protocol */
	public static Class<?> getContentbyClassName(String classname) throws ContentEx
	{
		Iterator<String> content_key;
		
		content_key = content_list.keySet().iterator();
		try { 
		while(content_key.hasNext()) {
			String key_name;
			Class<?> value;

			key_name = content_key.next();	
			value = content_list.get(key_name);
			if(value.getName().toLowerCase().endsWith("." + classname.toLowerCase())) {
				return value;	
			}
		}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return null;
	}
	
	public static Content insContent(String format) throws ContentEx
	{
		try { 
			Class<?> cls;
		
			cls = content_list.get(format);
			if(cls != null)
				return (Content)cls.newInstance();
		
		} catch(Exception ex) {
			throw new ContentEx(ex);
		}
	
		return null;
	}

	/* get a Content class from url protocol */
	public static Content getContent(String format) throws ContentEx
	{
		Content content;
		int sep;
		
		format = format.toUpperCase();
	
		/* instance by name content */
		content = insContent(format);
		if(content != null)
			return content;
		
		if((sep = format.indexOf('/')) > -1) {
			String type1, type;

			type1 = format.substring(0, sep);
			type = format.substring(sep + 1, format.length());


			/* instance by type content */
			content = insContent(type1 + "/*");
			if(content != null)
				return content;
			
			/* instance by format content */
			content = insContent(type);
			if(content != null)
				return content;
	
		}

		/* return default content object */
		return new Content();
	}
	
	/* register a content class */
	public static void registerContent(String format, Class<?> content_class) throws ContentEx
	{
		format = format.toUpperCase();
		
		if(content_list.get(format) != null)
			throw new ContentEx("content already was registed (" + format + ")");
		
		content_list.put(format, content_class);
	}
}
